Why does Ordered[A] use a compare method instead of reusing compareTo?
Posted
by
soc
on Stack Overflow
See other posts from Stack Overflow
or by soc
Published on 2010-12-23T18:14:01Z
Indexed on
2010/12/23
18:54 UTC
Read the original article
Hit count: 173
trait Ordered[A] extends java.lang.Comparable[A] {
def compare(that: A): Int
def < (that: A): Boolean = (this compare that) < 0
def > (that: A): Boolean = (this compare that) > 0
def <= (that: A): Boolean = (this compare that) <= 0
def >= (that: A): Boolean = (this compare that) >= 0
def compareTo(that: A): Int = compare(that)
}
Isn't it a bit useless to have both compare
and compareTo
?
What is the huge benefit I'm missing here?
If they had just used compareTo
I could just had replaced Comparable
with Ordered
in my code and be done.
© Stack Overflow or respective owner